from sqlalchemy.orm import sessionmaker
from models import Student
from config import Config
from sqlalchemy import create_engine


engine = create_engine(Config.SQLALCHEMY_DATABASE_URI)
Session = sessionmaker(bind=engine)


def add_student(sno, sname, ssex, sage, dno, sclass, address):
    session = Session()
    student = Student(Sno=sno, Sname=sname, Ssex=ssex, Sage=sage, Dno=dno, Sclass=sclass, address=address)
    session.add(student)
    session.commit()
    session.close()


def delete_student_by_sno(sno):
    session = Session()
    student = session.query(Student).filter_by(Sno=sno).first()
    if student:
        session.delete(student)
        session.commit()
    session.close()


def query_student_by_name(name):
    session = Session()
    students = session.query(Student).filter(Student.Sname.like('%' + name + '%')).all()
    session.close()
    return students